﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UCS.ArtificialInteligence.AI
{
    public class BlindSearch
    {
        #region Private Members

        private IProblem _problem;
        private List<IProblemState> _states;
        private int _totalWeight = 0;

        #endregion

        #region Constructors

        public BlindSearch(IProblem problem)
        {
            _problem = problem;
        }

        #endregion

        #region Public Methods

        public List<IProblemState> FindFirst(IAIList<ProblemStateBorder> list)
        {
            _states = new List<IProblemState>();

            IProblemState initialState = _problem.InitialState;
            _states.Add(initialState);

            if (_problem.IsFinalState(initialState))
                return _states;

            foreach (var border in initialState.Borders)
                list.Add(border);

            ProblemStateBorder currentBorder;
            IProblemState currentState;

            do
            {
                currentBorder = list.Remove();
                currentState = currentBorder.State;

                if (!_states.Contains(currentState))
                {
                    _states.Add(currentState);

                    if (_problem.IsFinalState(currentState))
                        return _states;

                    foreach (var border in currentBorder.State.Borders)
                        list.Add(border);
                }
            }
            while (currentBorder != null);

            return _states;
        }

        #endregion
    }
}